package fun.boomcat.luckyhe.mirai.plugin.dougu;

import fun.boomcat.luckyhe.mirai.plugin.dougu.command.DouguCommand;
import fun.boomcat.luckyhe.mirai.plugin.dougu.config.ConfigOperation;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.permission.PermissionId;
import net.mamoe.mirai.console.permission.PermissionRegistryConflictException;
import net.mamoe.mirai.console.permission.PermissionService;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Dougu extends JavaPlugin {

    public static final Dougu INSTANCE = new Dougu();

    private Permission douguPerm;

    private Dougu() {
        super(new JvmPluginDescriptionBuilder(
                "luckyhe.dougu",
                "1.0"
        ).build());
    }

    @Override
    public void onEnable() {
        INSTANCE.getLogger().info("开始加载插件");
        loadPermissions();
        INSTANCE.getLogger().info("注册权限完成");
        loadCommands();
        INSTANCE.getLogger().info("注册指令完成");
    }

    @Override
    public void onLoad(@NotNull PluginComponentStorage $this$onLoad) {
        try {
            ConfigOperation.initConfigPath(getConfigFolder(), getResource("config.properties", StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadPermissions() {
        PermissionId douguPermId = new PermissionId("dougu", "command.dougu");
        try {
            douguPerm = PermissionService.getInstance().register(
                    douguPermId,
                    "道具相关指令",
                    Permission.getRootPermission()
            );
        } catch (PermissionRegistryConflictException e) {
            e.printStackTrace();
        }
    }

    private void loadCommands() {
        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        String[] douguCommandSecondaryNames = {"道具"};
        CommandManager.INSTANCE.registerCommand(new DouguCommand(
                this,
                "dougu",
                douguCommandSecondaryNames,
                commandPrefix + "dougu <操作>",
                "道具相关指令",
                douguPerm,
                false
        ), false);
    }

}
